package momo;

import java.io.File;
import java.io.IOException;

import com.jscape.inet.email.EmailMessage;
import com.jscape.inet.mime.Attachment;
import com.jscape.inet.mime.MimeException;
import com.jscape.inet.smtp.Smtp;
import com.jscape.inet.smtp.SmtpAdapter;
import com.jscape.inet.smtp.SmtpConnectedEvent;
import com.jscape.inet.smtp.SmtpDisconnectedEvent;
import com.jscape.inet.smtp.SmtpException;


/**
 * 
 * @author Mohamed.Mosa@lu.unisi.ch
 *
 * The EmailSender class receives all the nessecery information to send an email 
 * with a photo attachment from the photo manager. Where as this information is 
 * received from the user's input in the MailGui class. The image path is added
 * to the mail gui by clicking on the desired image.
 */

public final class EmailSender extends SmtpAdapter {

	public final void send(String host, String to, String from, String subject, 
						   String body,String image, String login, String pass) 
						   throws SmtpException, IOException, MimeException {

		//create Smtp object
		Smtp smtp = new Smtp(host);

		//capture Smtp event
		smtp.addSmtpListener(this);		 

		smtp.setDebug(true);

		//Start an smtp connection with the server
		smtp.connect();

		smtp.login(login, pass);


		//********Generate Email***********
		//create new email message object
		EmailMessage email = new EmailMessage();

		email.setTo(to);

		email.setFrom(from);

		email.setSubject(subject);

		email.setBody(body);

		//attach image
		email.addAttachment(new Attachment(new File(image)));

		smtp.send(email);

		smtp.disconnect();

	}

	//Smtp connect method
	public final void connect(SmtpConnectedEvent e) {
		System.out.println("Connected to " + e.getHostname());
	}

	//Smtp disconnect method
	public final void disconnect(SmtpDisconnectedEvent e) {
		System.out.println("Disconnected from " + e.getHostname());
	}

}